<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Monitor extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 20;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$this->_template_f = $this->config->item('template_f');
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('monitor/monitor_model');
		$this->load->language('monitor/monitor', $this->_lang);
	}
	
	function index()
	{
		$this->campaignBad();
	}
	
	function campaignBad()
	{
		//Get from date and to date
		//$fdate = $this->mycommon->getSaveSqlStr($this->input->get('fd',true));
		//$tdate = $this->mycommon->getSaveSqlStr($this->input->get('td',true));
		$fdate = $this->input->get('fd',true);
		$tdate = $this->input->get('td',true);
		if($tdate == '')
		{
			$tdate = date('Y-m-d');
			$fdate = new DateTime();
			$fdate->sub(new DateInterval('P7D'));
			$fdate = $fdate->format('Y-m-d');
		}
		//Get group id
		$groupid = $this->input->get('gid',true);
		if($groupid == '' || !is_numeric($groupid)) $groupid = -1;
		$groupid = (int)$groupid;
		//Get page
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		
		$data = array();
		$data['fromdate'] = $fdate;
		$data['todate'] = $tdate;
		$data['gid'] = $groupid;
		$data['lstGroup'] = $this->monitor_model->get_group_list();
		$items = $this->monitor_model->campaignBad_getAll($fdate,$tdate,$groupid,$page,$this->_numItem);
		$data['items'] = $items['items'];
		$data['pages'] = $items['pages'];
		$data['page'] = $page;
		$data['lang'] = $this->lang->line('monitor');

		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'monitor/campaign_bad_index', $data);
		
		$this->_loadFooter();
	}
	
	function userNoAction()
	{
		//Get page
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$items = $this->monitor_model->userNoAction_getAll($page, $this->_numItem);
		$data['page'] = $page;
		$data['pages'] = $items['pages'];
		$data['items'] = $items['items'];
		$data['stt'] = ($page - 1)* $this->_numItem;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'monitor/user_no_action_index', $data);
		
		$this->_loadFooter();
	}
	
	function bannerErrorList()
	{
		//Get page
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$data['arrLocation'] = $this->lang->line('common_lang');
		$data['arrLocation'] = $data['arrLocation']['arr_location'];
		$items = $this->monitor_model->bannerError_getAll($page, $this->_numItem);
		$data['page'] = $page;
		$data['pages'] = $items['pages'];
		$data['items'] = $items['items'];
		$data['stt'] = ($page - 1)* $this->_numItem;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'monitor/banner_error_list', $data);
		
		$this->_loadFooter();
	}
	
	function bannerNotRunList()
	{
		//Get page
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$data['arrLocation'] = $this->lang->line('common_lang');
		$data['arrLocation'] = $data['arrLocation']['arr_location'];
		$items = $this->monitor_model->bannerNotRun_getAll($page, $this->_numItem);
		$data['page'] = $page;
		$data['pages'] = $items['pages'];
		$data['items'] = $items['items'];
		$data['stt'] = ($page - 1)* $this->_numItem;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'monitor/banner_not_run_list', $data);
		
		$this->_loadFooter();
	}
	
	function campaignCompleted()
	{
		//Get page
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$data['arrLocation'] = $this->lang->line('common_lang');
		$data['arrLocation'] = $data['arrLocation']['arr_location'];
		$items = $this->monitor_model->campaignCompleted_getAll($page, $this->_numItem);
		$data['page'] = $page;
		$data['pages'] = $items['pages'];
		$data['items'] = $items['items'];
		$data['stt'] = ($page - 1)* $this->_numItem;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'monitor/campaign_completed_list', $data);
		
		$this->_loadFooter();
	}
	
	function adsOnlineMonitoring()
	{
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_cpanel', $data);
		$this->_loadFooter();
	}
	
	function zoneMonitoring()
	{
		$zoneid = trim($this->input->get('zoneid',true));
		
		$data = array();
		$data['zoneid'] = $zoneid;
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		if(is_numeric($zoneid))
		{
			$page = (int)$this->input->get('page', true);
			$limit = trim($this->input->get('limit', true));
			if($limit != 'all') $limit = 20;
			$typegroup = (int)$this->input->get('typegroup', true);
			$user = trim($this->input->get('user', true));
			$location = (int)$this->input->get('location', true);
			$typeview = (int)$this->input->get('typeview', true);
			if($typeview == 0) $typeview = 2;
			
			$data['typegroup'] = $typegroup;
			$data['rows'] = $this->monitor_model->online_monitoring_zone($zoneid, $typegroup, $user, $location, $page, ($limit == 'all' ? 10000000 : $limit), $typeview);
			if($data['rows'][3] == null)
			{
				unset($data['rows']);
			}
			else
			{
				$data['records'] = $data['rows'][2][0]->records;
				$data['summary'] = $data['rows'][0][0];
				$data['zoneInfo'] = $data['rows'][3][0];
				$data['rows'] = $data['rows'][1];
				$data['count_user'] = 0;
				$tmpUsr = array();
				for($i = 0; $i < count($data['rows']); $i++)
				{
					$data['rows'][$i]->filename = $this->mycommon->getBannerImagPath('', '', $data['rows'][$i]->filename, strtotime($data['rows'][$i]->createtime));
					if(!in_array($data['rows'][$i]->username, $tmpUsr))
					{
						$tmpUsr[] = $data['rows'][$i]->username;
						$data['count_user']++;
					}
				}
				
				//Phan trang
				if(is_numeric($limit))
				{
					$this->load->library('pagination');
					$config = array('base_url'	=>	site_url('monitor/zoneMonitoring/?zoneid='.$zoneid.'&typegroup='.$typegroup.'&user='.$user.'&location='.$location.'&typeview='.$typeview),
								'total_rows'	=>	$data['records'],
								'cur_page'		=>	$page,
								'per_page'		=>	$limit,
								'num_links'		=>	5,
								'page_query_string'		=>	true,
								'query_string_segment'	=>	'page'
					);
					$this->pagination->initialize($config);
					$data['page_link'] = $this->pagination->create_links();
				}
				$data['page'] = $page;
				$data['user'] = $user;
				$data['location'] = $location;
				$data['typeview'] = $typeview;
			}
		}
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_zone', $data);
		
		$this->_loadFooter();
	}
	
	function zoneBigDataMonitoring()
	{
		$zoneid = trim($this->input->get('zoneid',true));
		
		$data = array();
		$data['zoneid'] = $zoneid;
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		if(is_numeric($zoneid))
		{
			$page = (int)$this->input->get('page', true);
			$limit = trim($this->input->get('limit', true));
			if($limit != 'all') $limit = 20;
			
			$data['rows'] = $this->monitor_model->online_monitoring_zone_bigdata($zoneid, $page, ($limit == 'all' ? 10000000 : $limit));
			if($data['rows'][0] == null)
			{
				unset($data['rows']);
			}
			else
			{
				$data['records'] = $data['rows'][1][0]->records;
				$data['rows'] = $data['rows'][0];
				for($i = 0; $i < count($data['rows']); $i++)
				{
					$data['rows'][$i]->filename = $this->mycommon->getBannerImagPath('', '', $data['rows'][$i]->filename, strtotime($data['rows'][$i]->createtime));
				}
				
				//Phan trang
				if(is_numeric($limit))
				{
					$this->load->library('pagination');
					$config = array('base_url'	=>	site_url('monitor/zoneBigDataMonitoring/?zoneid='.$zoneid.($limit > 0 ? '' : '&limit='.$limit)),
								'total_rows'	=>	$data['records'],
								'cur_page'		=>	$page,
								'per_page'		=>	$limit,
								'num_links'		=>	5,
								'page_query_string'		=>	true,
								'query_string_segment'	=>	'page'
					);
					$this->pagination->initialize($config);
					$data['page_link'] = $this->pagination->create_links();
				}
				$data['page'] = $page;
			}
		}
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_zone_bigdata', $data);
		
		$this->_loadFooter();
	}
	
	function zoneScheduleBigDataMonitoring()
	{
		$zoneid = trim($this->input->get('zoneid',true));
		
		$data = array();
		$data['zoneid'] = $zoneid;
		$data['loc'] = (int)$this->input->get('loc', true);
		$data['typegroup'] = (int)$this->input->get('typegroup', true);
        $data['typeview'] = $typeview = $this->input->get('typeview')!='' ? $this->input->get('typeview'):2;
        $data['username'] = $username= $this->input->get('username')!='' ? $this->input->get('username'):'';
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		if(is_numeric($zoneid))
		{
			$page = (int)$this->input->get('page', true);
			$limit = trim($this->input->get('limit', true));
			if($limit != 'all') $limit = 20;
			
			$data['rows'] = $this->monitor_model->online_monitoring_zone_schedule_bigdata($zoneid, $data['typegroup'], $data['loc'],$username,$typeview, $page, ($limit == 'all' ? 10000000 : $limit));
            if($data['rows'][0] == null)
			{
				unset($data['rows']);
			}
			else
			{
				$data['records'] = $data['rows'][1][0]->records;
				$data['rows'] = $data['rows'][0];
				for($i = 0; $i < count($data['rows']); $i++)
				{
					$data['rows'][$i]->filename = $this->mycommon->getBannerImagPath('', '', $data['rows'][$i]->filename, strtotime($data['rows'][$i]->createtime));
				}
				
				//Phan trang
				if(is_numeric($limit))
				{
					$this->load->library('pagination');
					$config = array('base_url'	=>	site_url('monitor/zoneScheduleBigDataMonitoring/?zoneid='.$zoneid.'&loc='.$data['loc'].'&typegroup='.$data['typegroup'].($limit > 0 ? '' : '&limit='.$limit)),
								'total_rows'	=>	$data['records'],
								'cur_page'		=>	$page,
								'per_page'		=>	$limit,
								'num_links'		=>	5,
								'page_query_string'		=>	true,
								'query_string_segment'	=>	'page'
					);
					$this->pagination->initialize($config);
					$data['page_link'] = $this->pagination->create_links();
				}
				$data['page'] = $page;
			}
		}
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_zone_schedule_bigdata', $data);
		
		$this->_loadFooter();
	}
	
	function zoneChannelMonitoring()
	{
		$channelid = trim($this->input->get('channelid',true));
		if($channelid == '') $channelid = 0;
		
		$data = array();
		$data['channelid'] = $channelid;
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		if(is_numeric($channelid))
		{
			$page = (int)$this->input->get('page', true);
			$limit = trim($this->input->get('limit', true));
			if($limit != 'all') $limit = 20;
			
			$data['rows'] = $this->monitor_model->online_monitoring_zone_channel($channelid, $page, ($limit == 'all' ? 10000000 : $limit));
			if($data['rows'][0] == null)
			{
				unset($data['rows']);
			}
			else
			{
				$data['records'] = $data['rows'][1][0]->records;
				$data['rows'] = $data['rows'][0];
				//Phan trang
				if(is_numeric($limit))
				{
					$this->load->library('pagination');
					$config = array('base_url'	=>	site_url('monitor/zoneChannelMonitoring/?channelid='.$channelid.($limit > 0 ? '' : '&limit='.$limit)),
								'total_rows'	=>	$data['records'],
								'cur_page'		=>	$page,
								'per_page'		=>	$limit,
								'num_links'		=>	5,
								'page_query_string'		=>	true,
								'query_string_segment'	=>	'page'
					);
					$this->pagination->initialize($config);
					$data['page_link'] = $this->pagination->create_links();
				}
				$data['page'] = $page;
			}
		}
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_zone_channel', $data);
		
		$this->_loadFooter();
	}
	
	function campaignInQueueMonitoring()
	{
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		
		//list banner of campaign in queue
		$campaignid = trim($this->input->get('campaignid',true));
		if(is_numeric($campaignid) && $campaignid > 0)
		{
			$page = trim($this->input->get('page',true));
			if(!is_numeric($page) || $page < 0) $page = 0;
			$limit = 20;
			$data['campaignid'] = $campaignid;
			$data['campaignname'] = trim($this->input->get('campaignname',true));
			
			$data['rows'] = $this->monitor_model->online_monitoring_campaign_inqueue_banners($campaignid, $page, $limit);
			$data['records'] = $data['rows'][1][0]->records;
			$data['rows'] = $data['rows'][0];
			for($i = 0; $i < count($data['rows']); $i++)
			{
				$data['rows'][$i]->filename = $this->mycommon->getBannerImagPath('', '', $data['rows'][$i]->filename, strtotime($data['rows'][$i]->createtime));
			}
			
			//Phan trang
			$this->load->library('pagination');
			$config = array('base_url'	=>	site_url('monitor/campaignInQueueMonitoring/?campaignid='.$campaignid.'&campaignname='.$data['campaignname']),
						'total_rows'	=>	$data['records'],
						'cur_page'		=>	$page,
						'per_page'		=>	$limit,
						'num_links'		=>	5,
						'page_query_string'		=>	true,
						'query_string_segment'	=>	'page'
			);
			$this->pagination->initialize($config);
			$data['page_link'] = $this->pagination->create_links();
			$data['page'] = $page;
			
			$this->_loadHeader();
			$this->_loadMenu();
			$this->load->view($this->_template_f.'monitor/monitoring_campaign_inqueue_banners', $data);
			$this->_loadFooter();
			
			return ;
		}
		//End
		
		//List campaigns in queue
		$data['rows'] = $this->monitor_model->online_monitoring_campaign_inqueue();

		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_campaign_inqueue', $data);
		
		$this->_loadFooter();
	}
	
	function userBudgetExpenMonitoring()
	{
		$date = trim($this->input->get('date', true));
		if($date == '')
		{
			$date = new DateTime();
			$date = $date->format('Y-m-d');
		}
		$user = trim($this->input->get('user', true));
		$typegroup = (int)$this->input->get('typegroup', true);
		if($typegroup == 0) $typegroup = -1;	//default get all
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		$data['items'] = $this->monitor_model->online_monitoring_user_budget_expen($date, $user, $typegroup);
		
		$data['user'] = $user;
		$data['date'] = $date;
		$data['typegroup'] = $typegroup;
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_user_budget_expen', $data);
		
		$this->_loadFooter();
	}
	
	function userZoneNumdayMonitoring()
	{
		$zoneid = (int)$this->input->get('zoneid', true);
		$user = trim($this->input->get('user', true));
		
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
		$data['common_lang'] = $this->lang->line('common_lang');
		if($zoneid > 0)
		{
			$data['items'] = $this->monitor_model->online_monitoring_user_zone_numday($zoneid, $user);
		}
		
		$data['zoneid'] = $zoneid;
		$data['user'] = $user;
			
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_user_zone_numday', $data);
		$this->_loadFooter();
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
    
    function campaignInQueueMonitoringBigData()
	{
		$data = array();
		$data['lang'] = $this->lang->line('monitor');
        
		$data['fromdate'] = date('Y-m-d', strtotime ( '-7 day' , strtotime (date('Y-m-d'))));
		$data['todate'] = date('Y-m-d');
		
        //list banner of campaign in queue
		$campaignid = trim($this->input->get('campaignid',true));
		if(is_numeric($campaignid) && $campaignid > 0)
		{
			$page = trim($this->input->get('page',true));
			if(!is_numeric($page) || $page < 0) $page = 0;
			$limit = 20;
			$data['campaignid'] = $campaignid;
			$data['campaignname'] = trim($this->input->get('campaignname',true));
			
			$data['rows'] = $this->monitor_model->online_monitoring_campaign_inqueue_banners($campaignid, $page, $limit);
			$data['records'] = $data['rows'][1][0]->records;
			$data['rows'] = $data['rows'][0];
			for($i = 0; $i < count($data['rows']); $i++)
			{
				$data['rows'][$i]->filename = $this->mycommon->getBannerImagPath('', '', $data['rows'][$i]->filename, strtotime($data['rows'][$i]->createtime));
			}
			
			//Phan trang
			$this->load->library('pagination');
			$config = array('base_url'	=>	site_url('monitor/campaignInQueueMonitoring/?campaignid='.$campaignid.'&campaignname='.$data['campaignname']),
						'total_rows'	=>	$data['records'],
						'cur_page'		=>	$page,
						'per_page'		=>	$limit,
						'num_links'		=>	5,
						'page_query_string'		=>	true,
						'query_string_segment'	=>	'page'
			);
			$this->pagination->initialize($config);
			$data['page_link'] = $this->pagination->create_links();
			$data['page'] = $page;
			
			$this->_loadHeader();
			$this->_loadMenu();
			$this->load->view($this->_template_f.'monitor/monitoring_campaign_inqueue_banners', $data);
			$this->_loadFooter();
			
			return ;
		}
		//End
		//List campaigns in queue
		$data['rows'] = $this->monitor_model->online_monitoring_campaign_inqueue_bigdata();
        
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'monitor/monitoring_campaign_inqueue_bigdata', $data);
		
		$this->_loadFooter();
	}
}